//
//  MCGoodsManageCell.swift
//  StoreManage
//
//  Created by MC on 2017/12/20.
//  Copyright © 2017年 MC. All rights reserved.
//

import UIKit

protocol MCGoodsManageCellDelegate : NSObjectProtocol {
    func MCGoodsManageCellEdit(row:Int)
}


class MCGoodsManageCell: UITableViewCell {

    
    var currentRow = 0
    
    
    // 类型  0平台，1自营
    var type = 0 {
        didSet {
            if type == 1 { // 自营
                // 隐藏结算价格
                settleMoneyLabel.snp.remakeConstraints { (make) ->Void in
                    make.right.equalTo(countLabel.snp.left).offset(0)
                    make.centerY.equalTo(self)
                    make.width.equalTo(0)
                }
                
            } else {  // 平台
                if MCUserinfo_getIsBill() == "0" { // 不开票
                    settleMoneyLabel.text = MCString_judgemantDecimalsIsZero(figure: model.settlementPrice)
                } else {  // 开票
                    settleMoneyLabel.text = MCString_judgemantDecimalsIsZero(figure: model.taxSettlementPrice)
                }
            }
        }
    }
    
    
    
    
    var model = MCGoodsManageModel() {
        didSet {
            // 规格的展示
            saleLabel.text = MCString_judgemantDecimalsIsZero(figure: model.salePrice)
            settleMoneyLabel.text = MCString_judgemantDecimalsIsZero(figure: model.settlementPrice)
            countLabel.text = String(model.inventoryNum)
            selectedButton.isSelected = model.isSelected
            specLabel.text = model.specStr
        }
    }
    
    weak var delegate : MCGoodsManageCellDelegate?
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        backgroundColor = UIColor.white
        
        
        self.addSubview(selectedButton)
        selectedButton.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(self).offset(0)
            make.centerY.equalTo(self)
            make.size.equalTo(CGSize.init(width: 40, height: 40))
        }
        
        let width : CGFloat = 70

        
        self.addSubview(editButton)
        editButton.snp.remakeConstraints { (make) ->Void in
            make.right.equalTo(self).offset(0)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.width.equalTo(MCAdaptationModel(40))
        }
        
        self.addSubview(countLabel)
        countLabel.snp.remakeConstraints { (make) ->Void in
            make.right.equalTo(editButton.snp.left).offset(0)
            make.centerY.equalTo(self)
            make.width.equalTo(MCAdaptationModel(width))
        }
        
        self.addSubview(settleMoneyLabel)
        settleMoneyLabel.snp.remakeConstraints { (make) ->Void in
            make.right.equalTo(countLabel.snp.left).offset(0)
            make.centerY.equalTo(self)
            make.width.equalTo(MCAdaptationModel(width))
        }
        
        self.addSubview(saleLabel)
        saleLabel.snp.remakeConstraints { (make) ->Void in
            make.right.equalTo(settleMoneyLabel.snp.left).offset(0)
            make.centerY.equalTo(self)
            make.width.equalTo(MCAdaptationModel(width))
        }
        
        self.addSubview(specLabel)
        specLabel.snp.remakeConstraints { (make) ->Void in
            make.right.equalTo(saleLabel.snp.left).offset(0)
            make.left.equalTo(selectedButton.snp.right).offset(0)
            make.top.equalTo(self).offset(3)
            make.bottom.equalTo(self).offset(-3)
        }
        
        self.addSubview(lineView)
        lineView.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(self).offset(10)
            make.right.equalTo(self)
            make.bottom.equalTo(self)
            make.height.equalTo(1)
        }

        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func selectedButtonClicked() {
        selectedButton.isSelected = !selectedButton.isSelected
        model.isSelected = selectedButton.isSelected
    }
    
    @objc func editButtonClicked() {
        delegate?.MCGoodsManageCellEdit(row: currentRow)
    }
    
    
    
    
    lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.MCGray_light
        return view
    }()
    
    
    
    
    public lazy var selectedButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.setImage(#imageLiteral(resourceName: "GoodsManage_notSelected"), for: UIControlState.normal)
        button.setImage(#imageLiteral(resourceName: "GoodsManage_selected"), for: UIControlState.selected)
        button.addTarget(self, action: #selector(selectedButtonClicked), for: UIControlEvents.touchUpInside)
        return button
    }()
    
    
    
    public lazy var specLabel: UILabel = {
        let label = UILabel.init()
        label.font = MCFont(12)
        label.textColor = UIColor.MCGray
        label.textAlignment = NSTextAlignment.center
        label.numberOfLines = 0
        return label
    }()
    
    
    public lazy var saleLabel: UILabel = {
        let label = UILabel.init()
        label.font = MCFont(12)
        label.textColor = UIColor.MCGray
        label.textAlignment = NSTextAlignment.center
        return label
    }()
    
    public lazy var settleMoneyLabel: UILabel = {
        let label = UILabel.init()
        label.font = MCFont(12)
        label.textColor = UIColor.MCGray
        label.textAlignment = NSTextAlignment.center
        return label
    }()
    public lazy var countLabel: UILabel = {
        let label = UILabel.init()
        label.font = MCFont(12)
        label.textColor = UIColor.MCGray
        label.textAlignment = NSTextAlignment.center
        return label
    }()
    
    
    public lazy var editButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.setImage(#imageLiteral(resourceName: "GoodsManage_edit"), for: UIControlState.normal)
        button.addTarget(self, action: #selector(editButtonClicked), for: UIControlEvents.touchUpInside)
        return button
    }()

}
